package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
@Transactional
public class WmSensitveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitveService {

    /**
     * 分页条件查询敏感词列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult pageList(SensitiveDto dto) {
        //检查参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();

        //分页
        IPage page = new Page(dto.getPage(),dto.getSize());
        //条件匹配
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(dto.getName())){  //敏感词名称模糊匹配
            wrapper.like(WmSensitive::getSensitives, dto.getName());
        }
        //倒序排序
        wrapper.orderByDesc(WmSensitive::getCreatedTime);

        //查询返回结果
        page = page(page,wrapper);
        return new PageResponseResult(page);
    }

    /**
     * 新增敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult insert(WmSensitive wmSensitive) {
        //检查参数
        if (wmSensitive == null || StringUtils.isBlank(wmSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //检查敏感词是否存在
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (sensitive != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //不存在则新增
        wmSensitive.setCreatedTime(new Date());
        boolean save = save(wmSensitive);

        return save ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS)
                : ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult edit(WmSensitive wmSensitive) {
        //检查参数
        if (wmSensitive == null || StringUtils.isBlank(wmSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //检查敏感词名称是否重复
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (sensitive != null && sensitive.getId() != wmSensitive.getId()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //更新
        boolean update = updateById(wmSensitive);
        return update ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS)
                : ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }
}
